/****************************************************
          File Name:7-10.cpp
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Sat 20 Dec 2014 05:56:41 PM CST
*****************************************************/

#include<iostream>
using namespace std;

double add(double x, double y)
{
    return x + y;
}


double calculate(double x, double y, double (*add)(double, double))
{
    return add(x, y);
}

double call_cal(double x, double y)
{
    return calculate(x, y, add);
}

double (*padd[3])(double x, double y) = {call_cal, call_cal, call_cal}; // declare and initailize

int main(int argc, char *argv[])
{
   double x, y;
   if(argc == 2)
   {
       cout << "Using \"double (*f)(double x, double y)\"" << endl;
       for(;;)
       {
           cout << "Enter two numbers: ";
           cin >> x >> y;
           cout << calculate(x, y,add) << endl;
       }
   }
   else
   {
       cout << "Using \"double (*padd[3])(double x, double y)\" " << endl;
       double res;
       for(int i = 0; i < 3; i++)
       {
           cout << "Enter two numbers: ";
           cin >> x >> y;
           res =(*padd[i])(x, y);
           cout << "ans: " << res << endl;
       }
   }

  return 0;
}
